<template>
  <div class="login-container">
    <div class="login-card">
      <h2 class="login-title">用户登录</h2>

      <form @submit.prevent="handleSubmit" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            v-model="form.username"
            type="text"
            placeholder="请输入用户名"
            :class="{ error: errors.username }"
          />
          <span class="error-message" v-if="errors.username">{{
            errors.username
          }}</span>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :class="{ error: errors.password }"
          />
          <span class="error-message" v-if="errors.password">
            {{ errors.password }}
          </span>
        </div>

        <button type="submit" class="login-button" :disabled="loading">
          <span v-if="!loading">登录</span>
          <span v-else>登录中...</span>
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
//import { useUserStore } from '@/stores/user'
//import { showToast } from '@/utils/toast'

//const router = useRouter();
//const userStore = useUserStore();

// 表单数据
const form = ref({
  username: "",
  password: "",
  remember: false,
});

// 验证错误信息
const errors = ref({
  username: "",
  password: "",
});

// 加载状态
const loading = ref(false);

// 表单验证
const validateForm = () => {
  let isValid = true;

  // 重置错误信息
  errors.value = { username: "", password: "" };

  // 用户名验证
  if (!form.value.username.trim()) {
    errors.value.username = "请输入用户名";
    isValid = false;
  } else if (form.value.username.length < 4) {
    errors.value.username = "用户名长度不能小于4位";
    isValid = false;
  }

  // 密码验证
  if (!form.value.password) {
    errors.value.password = "请输入密码";
    isValid = false;
  } else if (form.value.password.length < 6) {
    errors.value.password = "密码长度不能小于6位";
    isValid = false;
  }

  return isValid;
};

// 提交表单
const handleSubmit = async () => {
  if (!validateForm()) return;

  loading.value = true;

  try {
    // 调用登录接口
    await userStore.login({
      username: form.value.username,
      password: form.value.password,
    });

    // 登录成功处理
    showToast("登录成功", "success");

    // 跳转到首页或来源页面
    const redirect = router.currentRoute.value.query.redirect || "/";
    router.push(redirect);

    // 如果需要记住我，可以在这里处理
    if (form.value.remember) {
      // 例如保存到localStorage
    }
  } catch (error) {
    showToast(error.message || "登录失败", "error");
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped lang="less">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  font-weight: 500;
  color: #555;
}

.form-group input {
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.form-group input.error {
  border-color: #ff4d4f;
}

.error-message {
  color: #ff4d4f;
  font-size: 0.875rem;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.5rem 0;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
}

.forgot-password {
  font-size: 0.875rem;
  color: #1890ff;
  text-decoration: none;
}

.login-button {
  padding: 0.75rem;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #40a9ff;
}

.login-button:disabled {
  background-color: #bae7ff;
  cursor: not-allowed;
}

.login-footer {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.875rem;
  color: #666;
}

.login-footer a {
  color: #1890ff;
  text-decoration: none;
}
</style>
<style scoped lang="less">
//首页右边导航栏
.chat-container {
  overflow: hidden;
  position: relative;
  .right-aside {
    position: relative;
    float: right;
    width: 360px;
    height: 100%;
    background: #fff;
    padding: 0 3px;
    border-left: 1px solid #f5f6fa;
    border-radius: 0 0 20px 0;
    display: flex;
    flex-direction: column;
    .aside-ai {
      flex: 1;
      padding: 20px;
      .tools-title {
        color: #c42017;
        font-size: 30px;
        text-align: center;
      }
      .tools-card {
        display: flex;
        flex-wrap: wrap;
        .card-item {
          padding: 10px;
          cursor: pointer;
          border-radius: 5px;
          &:hover {
            background: rgba(245, 183, 179, 0.8);
          }
        }
      }
    }
    .aside-robot {
      display: flex;
      align-items: start;
      cursor: pointer;
    }
  }
}
//智能体弹出框
.container-agents {
  z-index: 2;
  width: 100%;
  height: 100%;
  border: 2px solid yellow;
  position: absolute;
  border-radius: 20px 0 0 20px;
  transition: right 0.5s ease;
  background: #fff;
  &.active-open {
    visibility: visible;
    right: 0;
  }
  &.active-hidden {
    visibility: hidden;
    right: -100%;
  }
}
</style>
